{% extends "PlatBundle::layout.html.twig" %}


{% block title %}{% if id > 0 %}编辑{% else %}添加{% endif %}勋章{% endblock %}

{% block css %}
{% endblock %}


{% block top %}
    {#{{ dump() }}#}
    {{ parent() }}
{% endblock %}


{% block left_body_top %}
    {{ parent() }}
{% endblock %}


{% block left_body %}
    {{ parent() }}
{% endblock %}


    {% block right_body %}
    <div class="page-header">
        <div class="page-right-top">
            <div class="right-top">
                <span class="right-title">勋章管理</span>
            </div>
            <div class="top-nav top-nav-active">{% if id > 0 %}编辑{% else %}添加新{% endif %}勋章</div>
        </div>
        <div class="main-content">
            <div class="mes-content">

                <div class="main-mes-content">
                    <form id="comMesBtn">
                        <div class="com-list">

                            <div class="com-list-box">
                                <div class="com-left fl">勋章名称</div>
                                <input type="text" name="medal" value="{% if id > 0 %}{{ medal_name }}{% endif %}" class="pop-com-input banner-name" style="width: 560px;" placeholder="请输入勋章名称">
                            </div>

                            <input type="hidden" name="id" value="0">
                            <input type="hidden" name="thumb" value="">

                            <div class="com-list-box">
                                <div class="com-left fl">勋章图片</div>

                                <img src="{% if thumb is defined %}{{ asset(thumb)|default(asset('assets/img/icon/modal-defaults.png')) }} {% else %} {{ asset('assets/img/icon/modal-defaults.png') }} {% endif %}" width="140" height="140" class="medal-image fl">

                                <div class="banner-explain fl" style="margin: 38px 0 0 50px;">
                                    <label class="upload-btn banner-btn" for="upload_file">点击上传</label>

                                    <input type="file" id="upload_file">

                                    <div class="banner-text">建议图片尺寸60*60 请上传2M以内图片</div>
                                </div>

                                <div class="clearfix"></div>
                            </div>

                            <div class="clearfix"></div>
                            <div class="add-mem-box" style="margin-top: 40px;">
                                <div class="com-left fl"></div>
                                <div class="acc-opera-btn fl mem-btn p-first-btn">保存</div>
                                <div class="acc-opera-btn fl mem-cancel">取消</div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    {% endblock %}

{% block body %}{% endblock %}


{% block box %}

{% endblock %}

{% block javascript %}
<script type="text/javascript">
    $(function () {

        SidebarHighlighting(3);

        // 取消
        $(".mem-cancel").click(function(){
            location.href = "{{ path('plat_medal_list') }}";
        })

        /// ajax 上传图片
        var img_path = "{{ thumb|default('') }}";
        function uploadFile() {
            var _file = this;
            var data = new FormData();
            data.append('image', _file.files[0]);

            URL_fileShareUpload = "{{ path('api_public_upload_image') }}";
            $.ajax({
                url:URL_fileShareUpload,
                type: 'POST',
                dataType: 'json',
                processData: false,
                contentType: false,
                data: data,
                success: function (data) {
                    if(data.errorCode == 0){

                        img_path = data.data.path;
                        $("input[name='thumb']").val(img_path);
                        $(".medal-image").attr("src","{{ asset('') }}"+img_path);
                        popStatus(1, '上传成功', 1,'', true);
                    } else {
                        alert(data.message);
                        return false;
                    }
                }
            });
        }
        $("#upload_file").on("change", uploadFile);

        // 保存勋章
        $(".mem-btn").click(function(){

            $("input[name='thumb']").val(img_path);

            $("input[name='id']").val("{{ id }}");
            var prame = $("#comMesBtn").serializeObject();

            if($.trim(prame.medal) == ""){
                popStatus(2, '请输入勋章名称', 1,'', true);
                return;
            }

            if(prame.thumb == ""){
                popStatus(2, '请上传图片', 1,'', true);
                return;
            }

            $.post("{{ path('api_platform_edit_medal') }}",prame,function(data){
                console.log(data);
                if(data.errorCode == 0){

                    popStatus(1, data.message,1,"{{ path('plat_medal_list') }}",true);
                } else {
                    popStatus(2, data.message,1,"",true);
                }

            })

        })


    })
</script>
{% endblock %}